<!--
 * @Date: 2022-06-21 16:39:14
 * @Author: 陶子
 * @LastEditTime: 2023-07-05 11:10:24
 * @FilePath: \jk_one-stop_cloud_mobile_v1.0\src\views\warehouseManagement\warehouse\addWarehouse.vue
 * @pageName: 这是--(新建仓库)--页面
-->
<template>
  <div>
    <Navbar :appTitle="`${newOpenType=='add'?'新建':'编辑'}仓库`" key="2" />
    <div class="search-content wrap-apply"
      :style="{top: GLOBAL.currentEnvironment ? '0' : '',height: GLOBAL.currentEnvironment ? '100vh' : 'calc(100vh - 1.12rem)',paddingBottom: GLOBAL.currentEnvironment ? '0.66rem' : ''}">
      <van-cell-group style="margin-top: 0.1rem">
        <p class="label required FormTit"><span></span>编号</p>
        <div class="haveUnitBox">
          <van-field v-model="buildForm.applyNumber" :maxLength='14' placeholder="请输入" @blur='checkInputValue(buildForm.applyNumber)' />
          <span class='btn-text' @click="genEntityCode">自动生成</span>
        </div>
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label required FormTit"><span></span>仓库名称</p>
        <van-field v-model="buildForm.warehouseName" placeholder="请输入" maxlength="30" />
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label required FormTit"><span></span>仓库分类</p>
        <van-field-select-picker label="" placeholder="请选择" v-model="buildForm.warehouseType" :columns="tabList" :option="{ label: 'text', value: 'value' }" />
        <div class="bottomLine"></div>
      </van-cell-group>

      <van-cell-group>
        <p class="label required FormTit"><span></span>仓库管理员</p>
        <van-field :is-link="!createdByName" readonly name="picker" v-model="createdByName" @click="ddthrVisible = true" placeholder="请选择">
          <div slot="right-icon" v-show="!!createdByName" @click.stop="confirmDdtjr('','',true)"><img src="~@/assets/img/filedClear.png" alt="" style="width:0.16rem">
          </div>
        </van-field>
        <div class="bottomLine"></div>
      </van-cell-group>

      <div style="margin-top: 0.12rem" v-if="showExam">
        <Examine @submitProcess="submitProcess" @getUserState="getUserState" type="0" :key="examKey" id="WMS_WAREHOUSE" ref="examine" />
        <div style="height: 0.26rem"></div>
      </div>
      <div class="search-bottoms">
        <span class="btnconfirm" @click="btnConfirm()">提交</span>
      </div>
    </div>
    <contact-pickers :visible="ddthrVisible" :max="5" @closePicker="ddthrVisible = false" @confirm="confirmDdtjr">
    </contact-pickers>
  </div>
</template>
<script>
import { getAction } from "@/api/manage";
import upload from "@/components/upload/upload";
import { clearNoNum, validateDuplicateValue, accAdd } from "@/utils/util";
import Navbar from "@/components/navBar/navBar_right";
import VanFieldSelectPicker from "@/components/ActionSheet/VanFieldSelectPicker";
import Examine from "@/components/examine";
import ContactPickers from "@/components/choiceuse/ContactPickers";
import { stockListApi, batchStockApi, queryGoodApi } from "@/api/mobile";
import { APILX } from "@/api/requestLx";
import moment from "moment";
export default {
  name: "search",
  components: {
    VanFieldSelectPicker,
    Navbar,
    upload,
    Examine,
    ContactPickers,
  },
  beforeRouteLeave(to, from, next) {
    if (this.showPeopleSlect) {
      this.$refs.examine.closePicker();
      next(false);
    } else {
      next();
    }
  },
  data() {
    return {
      ddthrVisible: false,
      newOpenType: "add",
      buildForm: {
        applyNumber: "",
        warehouseName: "",
        warehouseType: "",
        administerUserId: "",
      },
      tabList: [],
      examKey: 1,
      showPeopleSlect: false,
      showExam: true,
      editInfo: {},
      createdByName: "", //订单提交人
    };
  },
  created() {
    this.newOpenType = this.$route.query.openType || 'add';
    if (this.newOpenType == "edit") {
      this.showExam = false;
      let editInfo = this.$route.query.editInfo;
      console.log("看看是：", editInfo);
      this.buildForm = {
        applyNumber: editInfo.applyNumber,
        warehouseName: editInfo.warehouseName,
        warehouseType: editInfo.warehouseType,
        id: editInfo.id,
        administerUserId: editInfo.administerUserId,
      };
      this.createdByName = editInfo.administerUserName;
    } else {
      this.showExam = true;
      this.buildForm = {
        applyNumber: "",
        warehouseName: "",
        warehouseType: "",
      };
    }
    this.getInitData();
  },
  mounted() {},
  methods: {
    confirmDdtjr(val, selectedList, judge) {
      this.buildForm.administerUserId = "";
      this.createdByName = "";
      if (judge) {
        this.ddthrVisible = false;
        return;
      }
      this.buildForm.administerUserId = val ? val : ""; //人员id
      let arr = [];
      if (selectedList) {
        selectedList.forEach((item) => {
          arr.push(item.realname);
        });
      }
      this.createdByName = arr.join(",");
      this.ddthrVisible = false;
    },
    getUserState(v) {
      this.showPeopleSlect = v;
    },
    genEntityCode() {
      getAction("/sys/common/genEntityCode", {
        entityCode: "WMS_WAREHOUSE",
      }).then((res) => {
        if (res.code == 200) {
          this.buildForm.applyNumber = res.result;
        } else {
          this.buildForm.applyNumber = "";
          this.$toast(res.message);
        }
      });
    },
    // 提交
    btnConfirm() {
      if (!this.buildForm.applyNumber) {
        this.$toast(`请输入编号`);
        return;
      }
      if (!this.buildForm.warehouseName) {
        this.$toast(`请输入仓库名称`);
        return;
      }
      if (!this.buildForm.warehouseType) {
        this.$toast(`请选择仓库分类`);
        return;
      }
      if (!this.buildForm.administerUserId) {
        this.$toast(`请选择仓库管理员`);
        return;
      }
      if (this.newOpenType == "add") {
        this.submitProcess();
      } else {
        this.handleSubmit();
      }
    },
    submitProcess(e) {
      if (e && e.nodeDtos) {
        // 获取到数据后写表单提交的方法
        this.handleSubmit(e);
      } else {
        // 调用子组件的方法获取数据
        this.$refs.examine.confirm();
      }
    },
    handleSubmit(processDataVo) {
      let params = {
        ...this.buildForm,
      };
      if (this.newOpenType == "add") {
        params.processDataVo = processDataVo;
      }
      let requestUrl =
        this.newOpenType == "add"
          ? APILX.wmsWarehouseAdd
          : APILX.wmsWarehouseEdit;
      requestUrl(params).then((res) => {
        if (res.success) {
          this.$toast(res.message);
          this.$router.go(-1);
        } else {
          this.$toast(res.message);
        }
      });
    },
    checkInputValue(value) {
      validateDuplicateValue(
        "WMS_WAREHOUSE",
        "apply_number",
        value,
        this.newOpenType == "add" ? "" : this.buildForm.id,
        (res) => {
          if (res) {
            this.buildForm.applyNumber = "";
            this.$toast(res);
          }
        }
      );
    },
    getInitData() {
      APILX.wmsWarehouseGetTypeList().then((res) => {
        if (res.code == 200) {
          this.tabList = res.result;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/theme.scss";
@import "@/views/workManagement/materialRequisition/materialForm.scss";
.search-content {
  //  padding-bottom: 0.5rem;
  position: fixed;
  top: 0.42rem;
  width: 100%;
  height: calc(100vh - 1rem);
  background: #f5f5f5;
  overflow: scroll;
  z-index: 99;
  .formgroup {
    background: #ffffff;
    // padding: 0 0.16rem;
    margin-top: 0.1rem;
    margin-bottom: 0.2rem;
    padding-top: 0.1rem;

    .title {
      width: 0.58rem;
      height: 0.14rem;
      font-size: 0.15rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #191f25;
      line-height: 0.05rem;
      margin-left: 0.16rem;
    }
  }
  .search-bottoms {
    width: 100%;
    height: 0.5rem;
    background: #ffffff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    bottom: 0px;
    padding: 0rem 0.11rem;
    box-sizing: border-box;
    font-size: 0.16rem;
    font-weight: 500;
    z-index: 9;
    .btnconfirm {
      flex: 1;
      background: #1890ff;
      box-sizing: border-box;
      height: 0.39rem;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.03rem;
      color: #ffffff;
    }
  }
}
.bottomLine {
  height: 0.01rem;
  background-color: #f0f0f0;
  margin: 0 0.16rem;
}
.otherText {
  font-size: 0.16rem;
  font-weight: bold;
  color: #191f25;
}
.haveUnitBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  padding-right: 0.16rem;
  /deep/ .van-cell {
    padding-right: 0;
    width: 2.6rem;
  }
  /deep/ .van-field__control {
    max-width: 3rem;
  }
}
/deep/ .van-nav-bar {
  z-index: 99;
}
</style>
